<?php

namespace Biz\service\advertisement\apiModule;

use Doraemon\service\position\PositionConstInterface;
use Doraemon\tools\BlockType;
use Doraemon\tools\ImgUrl;

class BroadcastWithPos extends BaseModule
{

    const FORMAT_HIT = 'formatPosHit';

    const NOTE_PUBLISH = 0;
    const DIARY_PUBLISH = 1;
    const EDITOR_COPY_PUBLISH_MAP = [
        self::NOTE_PUBLISH => '动态',
        self::DIARY_PUBLISH => '装修日记',
    ];
    /**
     * api格式化输出
     * @param $hits
     * @param $uid
     * @param $userProfileExt
     * @param $statSignExt
     * @return array
     */
    public function format($hits, $uid, $userProfileExt, $statSignExt)
    {
        $banner_list = [];
        $response = [
            'type' => BlockType::TYPE_BROADCAST,
            'list' => &$banner_list,
        ];

        if ($hits) {
            if ($this->position_id == self::SEARCH_PRE_WORD) {
                $top = $hits['top'] ?? [];
                $rs = $hits['response'] ?? [];
                foreach ($top as $item) {
                    $banner_list[] = [
                        'p' => $item['ext_type'],
                        'pos' => $item['pos'],
                        'word' => $item['title'],
                    ];
                }
      
                foreach ($rs as $list) {
                    foreach ($list as $item) {
                        $banner_list[] = [
                            'p' => $item['ext_type'],
                            'pos' => $item['pos'],
                            'word' => $item['title'],
                        ];
                    }
                }
                return $banner_list;
            }
            $i = 1;
            $sort = array_column($hits, 'pos');
            array_multisort($sort, SORT_ASC, $hits);
            $length = 2;
            foreach ($hits as $hit) {
                //公共信息赋值
                $id = intval($hit['id']);
                $index = $i;
                switch ($hit['position_id']) {
                    case self::EDITOR_COPY:
                        $banner_list[$hit['pos']] = $hit['pic_id'];
                        break;
                    default:
                        $banner_list[] = [
                            'id' => $id,
                            'index' => $index,
                            'icon' => empty($hit['pic_id']) ? '' : ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $hit['pic_id'], ImgUrl::IMAGE_SIZE_OP_ORI),
                            'title' => $hit['title'],
                            'link' => $this->generateLink($hit['link_type'], $hit['link_params']),
                            'statSign' => $this->statSign($hit, $statSignExt),
                        ];
                        break;
                }
                $i++;
            }
            $banner_list = array_slice($banner_list, 0, $length);
        }
        if ($this->position_id == self::EDITOR_COPY) return $banner_list;
        return $response;
    }


    public function extCheckActive($v, $extInput)
    {
        $id = $extInput['id'] ?? 0;
        if ($this->position_id == self::SEARCH_PRE_WORD) return true;
        return $id == ($v['ext_type'] ?? 0);
    }

    public function uniquePos($v): string
    {
        return $this->position_id == self::SEARCH_PRE_WORD ? $v['pos'] . '_' . $v['ext_type'] : $v['pos'];
    }

    public function formatHits($hits)
    {
        if ($this->position_id == PositionConstInterface::SEARCH_PRE_WORD) return self::formatHitAllForSearchPreWord($hits);
        return parent::formatHits($hits);
    }
}